﻿using pachon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PachonDemo
{
    public partial class pcMian : Form
    {
        
        Comm com = new Comm();

        public pcMian()
        {
            pcDAL pd = new pcDAL();
            InitializeComponent();
            List<PC_INWEB> lpc = pd.GetPC_INWEB();
            dgv.AutoGenerateColumns = false;
            dgv.DataSource = lpc;
        }

        private void shuaxing_Click(object sender, EventArgs e)
        {
            butEnabled(false);
            pcDAL pd = new pcDAL();
            List<PC_INWEB> lpc = pd.GetPC_INWEB();
            dgv.AutoGenerateColumns = false;
            dgv.DataSource = lpc;
            MessageBox.Show("列表刷新成功...");
            butEnabled(true);
        }

        #region .
        private void button1_Click(object sender, EventArgs e)
        {
            //string str = GetUrlContent("http://labour-cn.com/ldhshbzjxxgk/xxgkzd/", Encoding.UTF8);
            //Regex reg = new Regex("<a[^>]+href=\"[^\"]+\"[^>]+title=\"[^>]+\">");
            //Regex reg2 = new Regex("/\\d[^\\.]+\\.htm");
            //var mat = reg.Matches(str);
            //List<string> mc = new List<string>();
            //foreach (Match a in mat)
            //{
            //    var aaaa = a.Groups[0].Value;
            //    var maat = reg2.Match(aaaa) + "";
            //    mc.Add(maat);
            //}
            //string strrr = "";
            //foreach (string a in mc)
            //{
            //    strrr = strrr + a + "\n";
            //}
            //richTextBox1.Text = strrr;
        }
        #endregion

        private void butEdit_Click(object sender, EventArgs e)
        {
            butEnabled(false);
            int rowsid = dgv.CurrentRow.Index;
            string rows_id = dgv.Rows[rowsid].Cells[0].Value + "";
            EditForm ef = new EditForm(rows_id);
            ef.ShowDialog(); 
            butEnabled(true);
        }

        private void butAdd_Click(object sender, EventArgs e)
        {
            butEnabled(false);
            AddForm af = new AddForm();
            af.ShowDialog(); 
            butEnabled(true);
        }

        int _icount = 0;

        public void ZXPachon()
        {
            //MessageBox.Show("开始执行爬虫中...");
            //this.Text = "开始执行爬虫中..."; 
            pcDAL pd = new pcDAL();
           // butEnabled(false);
            //this.progressBar1.Value = 50;
            var pcinweb = pd.GetPC_INWEB();
            var pccontentregex = pd.GetPC_CONTENT_REGEX();
            List<pcModelControl> lisstr = (from a in pcinweb
                                           join b in pccontentregex on a.id equals b.weburl_id
                                           where a.cando.Equals("1")
                                           select new pcModelControl
                                           {
                                               id = a.id,
                                               inweburl = a.inweburl,
                                               endcode = a.endcode,
                                               webregex1 = a.webregex1,
                                               webregex2 = a.webregex2,
                                               webregex3 = a.webregex3,
                                               title_regex1 = b.title_regex1,
                                               title_regex2 = b.title_regex2,
                                               title_regex3 = b.title_regex3,
                                               time_regex1 = b.time_regex1,
                                               time_regex2 = b.time_regex2,
                                               time_regex3 = b.time_regex3,
                                               content_regex1 = b.content_regex1,
                                               content_regex2 = b.content_regex2,
                                               content_regex3 = b.content_regex3,
                                                appendurl=a.appendurl
                                           }).ToList();
             _icount = lisstr.Count();
            
            int _i = 1;
            //int _jj = 1;
            //int _j = 1;
            //获取入口网址还有相关的正则表达式
            foreach (pcModelControl pmc in lisstr)
            {
              //  labcount.Text = _i +"" ;
                GetUrlList(pmc);
                Thread.Sleep(5000);
                //progressBar1.Value = 100 * _i / _icount;
               // backgroundWorker1.ReportProgress(_i * 100 / _icount, new msg() { id=_i, content=pmc.inweburl, beizhu="" });
                _i++;
            }
            //this.Text = "爬虫";
            //butEnabled(true);
        }
       
        /// <summary>
        /// 保存数据列表到数据库
        /// </summary>
        /// <param name="pcc"></param>
        public void Insert_intoInfoList(List<pcModelContents> listpmlc)
        {
            if (listpmlc.Count() > 0)
            {
                pcDAL pd = new pcDAL();
                List<PC_CONTENT> listPCC = new List<PC_CONTENT>();
                foreach (pcModelContents pmlc in listpmlc)
                {
                    PC_CONTENT pcc = new PC_CONTENT();
                    pcc.id = com.RandCode(8);
                      pcc. weburl   = pmlc.weburl ;
                      pcc. title    = pmlc.title ;
                      pcc. webid    = pmlc.webid ;
                      pcc. content  = pmlc.content ;
                      pcc. time     = pmlc.time;
                      listPCC.Add(pcc);
                }
                pd.InsertPC_CONTENTList(listPCC);
            }
        }


        /// <summary>
        /// 保存数据到数据库
        /// </summary>
        /// <param name="pcc"></param>
        public void Insert_intoInfo(pcModelContents pmlc)
        {
            if (pmlc!=null)
            {
                pcDAL pd = new pcDAL();
                    PC_CONTENT pcc = new PC_CONTENT();
                    pcc.id = com.RandCode(8);
                    pcc.weburl = pmlc.weburl;
                    pcc.title = pmlc.title;
                    pcc.webid = pmlc.webid;
                    pcc.content = pmlc.content;
                    pcc.time = pmlc.time;
                    pd.InsertPC_CONTENT(pcc);
            }
        }

        public void butEnabled(bool bol)
        {
            butEdit.Enabled = bol;
            butAdd.Enabled = bol;
            btprun.Enabled = bol;
            shuaxing.Enabled = bol;
            butliji.Enabled = bol;
            butPlan.Enabled = bol;
           // txtmsg.Visible = bol;
            //progressBar2.Visible = bol == false ? true : bol;
        }

        #region 根据入口网址获取网址链接
        /// <summary>
        /// 根据入口网址获取网址链接
        /// </summary>
        /// <param name="pmc"></param>
        public void GetUrlList(pcModelControl pmc)
        {
            Encoding ec = Encoding.UTF8;
            if (pmc.endcode.Equals("gb2312"))
                ec = Encoding.Default;
            try
            {
                string htmlStr = com.GetUrlContent(pmc.inweburl, ec);
                if (htmlStr != null)
                {
                    GetWydzlb(pmc, htmlStr);
                }
            }
            catch
            {
                //listpmlc = null;
                com.WriteToLog("请检查入口网址是否错误！");
            }
        } 
        #endregion


        private void GetWydzlb(pcModelControl pmc, string htmlStr)
        {
            try
            {
                int _j = 0;
                List<string> listUrlStr = com.GetRegexString(htmlStr, pmc.webregex1, pmc.webregex2, pmc.webregex3, pmc.appendurl);
                int _jj = listUrlStr.Count;
                foreach (string urlstr in listUrlStr)
                {
                    
                    if (UrlHasRuned(urlstr) == false)
                    {
                        GetContents(urlstr, pmc);
                        Thread.Sleep(1000);
                    }
                  
                    _j++;

                    if (_methBool==false)
                    {
                        backgroundWorker1.ReportProgress(_j * 100 / _jj, new msg() { id = _j, content = urlstr, beizhu = "" });
                    }
                    else
            {
                progressBar2.Value = 100 * _j / _jj;
            }
                } 
               
            }
            catch
            {
                com.WriteToLog("获取网页地址的列表错误！");
            }
            
        }


        #region 获取文章的标题,时间,内容
        /// <summary>
        /// 获取文章的标题,时间,内容
        /// </summary>
        /// <param name="urlstr"></param>
        /// <param name="pmc"></param>
        /// <param name="title"></param>
        /// <param name="time"></param>
        /// <param name="content"></param>
        public void GetContents(string urlstr, pcModelControl pmc)
        {
            Encoding ec = Encoding.UTF8;
            if (pmc.endcode.Equals("gb2312"))
                ec = Encoding.Default;
            string htmlStr = GetNeironHtml(urlstr,ec);
            if (htmlStr != null)
            {
                ChaRuContentS(urlstr, htmlStr, pmc);//插入内容
            }
        } 
        #endregion


        private string GetNeironHtml(string urlstr,Encoding ec)
        {
            string htmlStr = null;
            try
            {
                htmlStr = com.GetUrlContent(urlstr, ec);
            }
            catch
            {
                htmlStr = null;
                com.WriteToLog("请检查网页列表地址是否错误！");
            }
            return htmlStr;
        }

        
        private void ChaRuContentS(string urlstr, string htmlStr, pcModelControl pmc)
        {
            pcModelContents pmlc = new pcModelContents();

            pmlc.webid = pmc.id;
            pmlc.creattime = System.DateTime.Now;
            pmlc.title = GetTitle(htmlStr, pmc); //获取标题 
            pmlc.time = GetTime(htmlStr, pmc);
            pmlc.content = GetContent(htmlStr, pmc);
            pmlc.weburl = urlstr;

            Insert_intoInfo(pmlc);

            //Thread.Sleep(1000);
            //MessageBox.Show(pmc.inweburl);
        }



        #region 获取标题
        /// <summary>
        /// 获取标题
        /// </summary>
        /// <param name="htmlStr"></param>
        /// <param name="pmc"></param>
        /// <returns></returns>
        private string GetTitle(string htmlStr, pcModelControl pmc)
        {
            string title = null;
            try
            {
                List<string> listUrlStrtitle = com.GetRegexString(htmlStr, pmc.title_regex1, pmc.title_regex2, pmc.title_regex3, null);
                title = listUrlStrtitle.FirstOrDefault();
            }
            catch
            {
                title = null;
                com.WriteToLog("获取网页标题错误！");
            }
            return title;
        } 
        #endregion

        #region 获取时间
        /// <summary>
        /// 获取时间
        /// </summary>
        /// <param name="htmlStr"></param>
        /// <param name="pmc"></param>
        /// <returns></returns>
        private string GetTime(string htmlStr, pcModelControl pmc)
        {
            string time = null;
            try
            {
                List<string> listUrlStrtime = com.GetRegexString(htmlStr, pmc.time_regex1, pmc.time_regex2, pmc.time_regex3, null);
                time = listUrlStrtime.FirstOrDefault();
            }
            catch
            {
                time = null;
                com.WriteToLog("获取网页时间错误！");
            }
            return time;
        } 
        #endregion

        #region 获取内容
        /// <summary>
        /// 获取内容
        /// </summary>
        /// <param name="htmlStr"></param>
        /// <param name="pmc"></param>
        /// <returns></returns>
        private string GetContent(string htmlStr, pcModelControl pmc)
        {
            string content = null;
            try
            {
                List<string> listUrlStrcontent = com.GetRegexString(htmlStr, pmc.content_regex1, pmc.content_regex2, pmc.content_regex3, null);
                content = listUrlStrcontent.FirstOrDefault();
            }
            catch
            {
                content = null;
                com.WriteToLog("获取网页内容错误！");
            }
            return content;
        } 
        #endregion

        #region 有跑过的URL地址
        /// <summary>
        /// 有跑过的URL地址
        /// </summary>
        /// <param name="strUrl"></param>
        /// <returns></returns>
        public bool UrlHasRuned(string strUrl)
        {
            bool _bol = false;
            pcDAL pd = new pcDAL();
            List<string> listRunedUrl = pd.GetPC_CONTENTRunedUrl();
            foreach (string sUrl in listRunedUrl)
            {
                if (sUrl.Equals(strUrl) == true)
                {
                    _bol = true;

                }
               
            }
            return _bol;
        }
        #endregion

        private void btprun_Click(object sender, EventArgs e)
        {
            butEnabled(false);
            PlanFrom pf = new PlanFrom();
            pf.ShowDialog();
            butEnabled(true);
        }
        bool _methBool = false;

        private void butliji_Click(object sender, EventArgs e)
        {
            _methBool = false;
            butEnabled(false);
            this.Text = "开始执行爬虫中...";
            this.backgroundWorker1.RunWorkerAsync(); 
            txtmsg.Visible = true;
        }


        private void butPlan_Click(object sender, EventArgs e)
        {
            _methBool = true;
            butEnabled(false);
            this.Text = "开始执行爬虫中...";
            
            progressBar2.Visible = true;
            DoPlany();
            //progressBar2.Visible = false;
           
            //this.backgroundWorker1.RunWorkerAsync(); 
            txtmsg.Visible = true;
            //
        }
       System.Timers.Timer myTimer = new System.Timers.Timer();
        private void DoPlany()
        {
            myTimer.Interval =  5000;//(一分钟执行一次)
            myTimer.Enabled = true;
            myTimer.Elapsed += new System.Timers.ElapsedEventHandler(AceptPlany);
        }

        bool _cando = true;//用于任务没有执行完成是不予许再次判断时间
        private void AceptPlany(object source,System.Timers.ElapsedEventArgs e)
        {
            pcModelPlan pcModelPlanTime = GetpcModelPlanTime();
            if (pcModelPlanTime != null)
            {
                if (IsThisMoth(pcModelPlanTime))
                {
                    if (IsThisWeek(pcModelPlanTime))
                    {
                        if (IsThisHost(pcModelPlanTime))
                        {
                            if (_cando)
                            {
                                _cando = false;

                                ZXPachon(); 

                                Thread.Sleep(5000);
                                //MessageBox.Show("sss");
                                _cando = true;
                            }
                        }
                    }
                }
                //MessageBox.Show("sss");
            } 
        }

        #region 时间处理
        //取当前年月日时分秒      currentTime=System.DateTime.Now; 
        //取当前年     int 年=currentTime.Year; 
        //取当前月     int 月=currentTime.Month; 
        //取当前日     int 日=currentTime.Day; 
        //取当前时     int 时=currentTime.Hour; 
        //取当前分     int 分=currentTime.Minute; 
        //取当前秒     int 秒=currentTime.Second; 
        //取当前毫秒   int 毫秒=currentTime.Millisecond; （变量可用中文）
        //（int）DateTime.Now.DayOfWeek     数字，若是周三，结果对应为3 
        #endregion

        /// <summary>
        /// 判断月份是否可以执行
        /// </summary>
        /// <param name="PlanTime"></param>
        /// <returns></returns>
        private bool IsThisMoth(pcModelPlan planTime)
        {
            bool _mothBol = false;
            DateTime dt = System.DateTime.Now;
            int moth = dt.Month;

            List<int> listint = new List<int>();

            if (planTime.cbmoth1)
            {
                listint.Add(1);
            }
            if (planTime.cbmoth2)
            {
                listint.Add(2);
            }
            if (planTime.cbmoth3)
            {
                listint.Add(3);
            }
            if (planTime.cbmoth4)
            {
                listint.Add(4);
            }
            if (planTime.cbmoth5)
            {
                listint.Add(5);
            }
            if (planTime.cbmoth6)
            {
                listint.Add(6);
            }
            if (planTime.cbmoth7)
            {
                listint.Add(7);
            }
            if (planTime.cbmoth8)
            {
                listint.Add(8);
            }
            if (planTime.cbmoth9)
            {
                listint.Add(9);
            }
            if (planTime.cbmoth10)
            {
                listint.Add(10);
            }
            if (planTime.cbmoth11)
            {
                listint.Add(11);
            }
            if (planTime.cbmoth12)
            {
                listint.Add(12);
            }
            foreach (int ii in listint)
            {
                if (ii == moth)
                {
                    _mothBol = true;
                }
            }
            return _mothBol;
        }

        /// <summary>
        /// 判断星期是否可以执行
        /// </summary>
        /// <param name="PlanTime"></param>
        /// <returns></returns>
        private bool IsThisWeek(pcModelPlan planTime)
        {
            bool _weekBol = false;
            DateTime dt = System.DateTime.Now;
            int week = (int)dt.DayOfWeek;
            List<int> listweek=new List<int> ();

           if( planTime.cbxinqi1)
           {
               listweek.Add(1);
           }
            if( planTime.cbxinqi2)
           {
               listweek.Add(2);

           }
            if( planTime.cbxinqi3)
           {
               listweek.Add(3);

           }
            if( planTime.cbxinqi4)
           {
               listweek.Add(4);

           }
            if (planTime.cbxinqi5)
            {
                listweek.Add(5);

            }
            if( planTime.cbxinqi6)
           {
               listweek.Add(6);

           }
            if( planTime.cbxinqi7)
           {
               listweek.Add(0);

           }

            foreach (int ii in listweek)
            {
                if (ii == week)
                {
                    _weekBol = true;
                }
            }

            return _weekBol;
        }

        /// <summary>
        /// 判断小时是否可以执行
        /// </summary>
        /// <param name="PlanTime"></param>
        /// <returns></returns>
        private bool IsThisHost(pcModelPlan planTime)
        {
            bool _hostBol = false;
            DateTime dt = System.DateTime.Now;
            int hour = dt.Hour;
            List<int> listhour = new List<int>();

            if (planTime.cbhost1) { listhour.Add(1); }
            if (planTime.cbhost2) { listhour.Add(2); }
            if (planTime.cbhost3) { listhour.Add(3); }
            if (planTime.cbhost4) { listhour.Add(4); }
            if (planTime.cbhost5) { listhour.Add(5); }
            if (planTime.cbhost6) { listhour.Add(6); }
            if (planTime.cbhost7) { listhour.Add(7); }
            if (planTime.cbhost8) { listhour.Add(8); }
            if (planTime.cbhost9) { listhour.Add(9); }
            if (planTime.cbhost10) { listhour.Add(10); }
            if (planTime.cbhost11) { listhour.Add(11); }
            if (planTime.cbhost12) { listhour.Add(12); }
            if (planTime.cbhost13) { listhour.Add(13); }
            if (planTime.cbhost14) { listhour.Add(14); }
            if (planTime.cbhost15) { listhour.Add(15); }
            if (planTime.cbhost16) { listhour.Add(16); }
            if (planTime.cbhost17) { listhour.Add(17); }
            if (planTime.cbhost18) { listhour.Add(18); }
            if (planTime.cbhost19) { listhour.Add(19); }
            if (planTime.cbhost20) { listhour.Add(20); }
            if (planTime.cbhost21) { listhour.Add(21); }
            if (planTime.cbhost22) { listhour.Add(22); }
            if (planTime.cbhost23) { listhour.Add(23); }
            if (planTime.cbhost24) { listhour.Add(0); }

            foreach (int ii in listhour)
            {
                if (ii == hour)
                {
                    _hostBol = true;
                }
            }

            return _hostBol;
        }

        /// <summary>
        /// 获取计划任务的时间设置
        /// </summary>
        /// <returns></returns>
        private pcModelPlan GetpcModelPlanTime()
        {
            pcModelPlan pmp = null;
            string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "plan.xml";
            pmp = (pcModelPlan)com.Load(typeof(pcModelPlan), filePath);
            return pmp;
        }

        private void butZhantin_Click(object sender, EventArgs e)
        {
            myTimer.Enabled = false;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("iexplore.exe", "http://regexr.com/");  
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Text = "爬虫";
            butEnabled(true);
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
                ZXPachon();
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            msg mssg = (msg)e.UserState;
            labcount.Text = mssg.id.ToString();
            txtmsg.Text += mssg.content+"\r\n";
        }

        private void butstop_Click(object sender, EventArgs e)
        {
            try {   
                this.backgroundWorker1.CancelAsync();
            }
            catch 
            { 
            }
        }

        #region .
        //public pcModelPlan setvalue(pcModelPlan pmp)
        //{
        //    pcModelPlan pm = new pcModelPlan();
        //   pm.cbxinqi1 = pmp.cbxinqi1;
        //   pm.cbxinqi2 = pmp.cbxinqi2;
        //   pm.cbxinqi3 = pmp.cbxinqi3;
        //   pm.cbxinqi4 = pmp.cbxinqi4;
        //   pm.cbxinqi5 = pmp.cbxinqi5;
        //   pm.cbxinqi6 = pmp.cbxinqi6;
        //   pm.cbxinqi7 = pmp.cbxinqi7;

        //    pm.cbmoth1 = pmp.cbmoth1;
        //    pm.cbmoth2 = pmp.cbmoth2;
        //    pm.cbmoth3 = pmp.cbmoth3;
        //    pm.cbmoth4 = pmp.cbmoth4;
        //    pm.cbmoth5 = pmp.cbmoth5;
        //    pm.cbmoth6 = pmp.cbmoth6;
        //    pm.cbmoth7 = pmp.cbmoth7;
        //    pm.cbmoth8 = pmp.cbmoth8;
        //    pm.cbmoth9 = pmp.cbmoth9;
        //    pm.cbmoth1 = pmp.cbmoth1;
        //    pm.cbmoth1 = pmp.cbmoth1;
        //    pm.cbmoth1 = pmp.cbmoth1;


        //    pm.cbhost1 = pmp.cbhost1;
        //    pm.cbhost2 = pmp.cbhost2;
        //    pm.cbhost3 = pmp.cbhost3;
        //    pm.cbhost4 = pmp.cbhost4;
        //    pm.cbhost5 = pmp.cbhost5;
        //    pm.cbhost6 = pmp.cbhost6;
        //    pm.cbhost7 = pmp.cbhost7;
        //    pm.cbhost8 = pmp.cbhost8;
        //    pm.cbhost9 = pmp.cbhost9;
        //    pm.cbhost10 = pmp.cbhost10;
        //    pm.cbhost11 = pmp.cbhost11;
        //    pm.cbhost12 = pmp.cbhost12;
        //    pm.cbhost13 = pmp.cbhost13;
        //    pm.cbhost14 = pmp.cbhost14;
        //    pm.cbhost15 = pmp.cbhost15;
        //    pm.cbhost16 = pmp.cbhost16;
        //    pm.cbhost17 = pmp.cbhost17;
        //    pm.cbhost18 = pmp.cbhost18;
        //    pm.cbhost19 = pmp.cbhost19;
        //    pm.cbhost20 = pmp.cbhost20;
        //    pm.cbhost21 = pmp.cbhost21;
        //    pm.cbhost22 = pmp.cbhost22;
        //    pm.cbhost23 = pmp.cbhost23;
        //    pm.cbhost24 = pmp.cbhost24;
        //    return pm;    
        //} 
        #endregion
    }
}
